﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;


namespace DataAccess
{
   public class Login
    {
       private string _userName;
       private string _userPwd;
       private string _userPhone;
       //get and set function
        #region
       public string UserName
       {
           get { return _userName; }
           set { _userName = value; }
       }

       public string UserPwd
       {
           get { return _userPwd; }
           set { _userPwd = value; }
       }
       public string UserPhone
       {
           get { return _userPhone; }
           set { _userPhone = value; }
       }
        #endregion

       public static bool LoginUser(string username,string userpwd)
       {
           string sql = "select * from users where userName = @username and userPwd = @userpwd";
           MySqlParameter[] commandParameters = {new MySqlParameter("@userName",username),new MySqlParameter("@userPwd",userpwd)};
           Object obj = MySqlHelper.ExecuteScalar(MySqlHelper.Conn, CommandType.Text, sql, commandParameters);
           if (obj == null)
               return false;

           return true;
       }

       public static int Registered(Login login)
       {
           string sql = "insert into users(userName,userPwd,userPhone) values(@username,@userpwd,@userphone)";
           MySqlParameter[] commandParameters = { new MySqlParameter("@userName", login._userName), new MySqlParameter("@userPwd", login._userPwd), new MySqlParameter("@userPhone", login._userPhone)};
           int result = MySqlHelper.ExecuteNonQuery(MySqlHelper.Conn, CommandType.Text, sql, commandParameters);
           return result;
       }

       public static bool IsUserExist(string userName)
       {
           string sql = "select * from users where userName = @userName";
           Object obj = MySqlHelper.ExecuteScalar(MySqlHelper.Conn, CommandType.Text, sql, new MySqlParameter("@userName", userName));
           if (obj == null)
               return false;
           else
               return true;
       }

    }
}
